package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.SysMenu;
import com.k2data.k2app.domain.SysRole;
import com.k2data.k2app.domain.vo.SimpleObject;
import com.k2data.k2app.mapper.SysRoleMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SysMenuService;
import com.k2data.k2app.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单
 * Created by clb on 17-3-17.
 */
@Api(description = "角色管理")
@RestController
@RequestMapping("/roles")
public class SysRoleController {
	private final SysRoleMapper sysRoleMapper;

	@Autowired
	private SysMenuService sysMenuService;

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	public SysRoleController(SysRoleMapper sysRoleMapper) {
		this.sysRoleMapper = sysRoleMapper;
	}


	@ApiOperation(value = "创建")
	@ApiImplicitParam(name = "sysRole", value = "sysRole对象", required = true, dataType = "SysRole", paramType = "body")
	@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> post(@RequestBody SysRole sysRole) throws Exception {

		return ResponseHelper.success(sysRoleService.insert(sysRole));
	}

	@ApiOperation(value = "根据 id 更新")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "sysRole", value = "sysRole对象", required = true, dataType = "SysRole", paramType = "body")
	})
	@PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> update(@PathVariable("id") Long id,
													   @RequestBody SysRole sysRole) throws Exception {
		sysRole.setId(id);
		return ResponseHelper.success(sysRoleService.update(sysRole));
	}

	@ApiOperation(value = "获取所有记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query")
	})
	@GetMapping(value = "")
	public ResponseEntity<CommonResultResponse> list(@RequestParam(value = "pageNum", required = false) Integer pageNum,
	                                                 @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
		pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
		pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
		int count = sysRoleMapper.selectCount(new SysRole());
		PageHelper.startPage(pageNum, pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize);
		PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);
		List<SysRole> list = sysRoleService.queryList();
		return ResponseHelper.success(list,pageInfo);
	}

	@ApiOperation(value = "获取所有记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query")
	})
	@GetMapping(value = "/simple")
	public ResponseEntity<CommonResultResponse> simple(@RequestParam(value = "pageNum", required = false) Integer pageNum,
	                                                   @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
		pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
		pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
		int count = sysRoleMapper.selectCount(new SysRole());
		PageHelper.startPage(pageNum, pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize);
		PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);
		List<SimpleObject> list = sysRoleService.getRolesSimple();
		return ResponseHelper.success(list,pageInfo);
	}

	@ApiOperation(value = "角色的菜单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "isTree", value = "是否返回树形结构(默认tree)", dataType = "Boolean", paramType = "query"),
			@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path")
	})
	@GetMapping(value = "/{id}/menus")
	public ResponseEntity<CommonResultResponse> menusById(@RequestParam(value = "pageNum", required = false) Integer pageNum,
														  @RequestParam(value = "pageSize", required = false) Integer pageSize,
														  @RequestParam(value = "isTree", required = false) Boolean isTree,
														  @PathVariable("id") Long id) throws Exception {
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
		PageHelper.startPage(pageNum, pageSize);
		List<SysMenu> list = sysMenuService.getMenusByRole(id,isTree);
		int count = list.size();
		PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);
		return ResponseHelper.success(list,pageInfo);
	}

	@ApiOperation(value = "删除")
	@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> updateDeletedFlag(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(sysRoleService.deleteById(id));
	}
}
